/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Nov 22, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control.gui;

import edu.psu.geovista.app.pcp.plot.control.AxisLocationCfg;
import edu.psu.geovista.app.pcp.plot.control.ConfigManager;
import edu.psu.geovista.app.pcp.plot.control.Settings;
import edu.psu.geovista.app.pcp.plot.control.tools.AxisPlaceTool;
import edu.psu.geovista.app.pcp.plot.control.tools.BasicTool;
import edu.psu.geovista.app.pcp.plot.control.tools.PlotTool;
import edu.psu.geovista.app.pcp.plot.layer.AxisLayer;
import edu.psu.geovista.app.pcp.toolkits.Validator;
import edu.psu.geovista.app.plot.axis.AxisList;
import edu.psu.geovista.app.plot.axis.XAxisOrder;
import edu.psu.geovista.app.plot.axis.YAxis;

public class AxisSizeSection extends javax.swing.JPanel {
    AxisLocationCfg config;
    /** Creates new form AxisAppearance */
    public AxisSizeSection(AxisLocationCfg config) {
        initComponents();
        this.initValue(config);
    }

    public void setAxisInterval(int interval){
        this.axisIntervalFd.setText(String.valueOf(interval));

    }
    public void setAxisHeight(int h){
        this.axisHeightFd.setText(String.valueOf(h));
    }
    /*******************************************************************************************************
     *               support
     *******************************************************************************************************/

    private   void initValue(AxisLocationCfg config) {
        //interval&height textfields
        this.config=config;
        AxisList yaxisList=config.getYaxisList() ;
        XAxisOrder xaxis=config.getXaxis() ;
        YAxis axis0=yaxisList.getAxis(0);
        if(axis0!=null){
            this.axisHeightFd.setText(axis0.getActiveSize()+"" );
        }
        this.axisIntervalFd.setText(xaxis.getDefaultInterval() +"");


        //showXXX checkbox
        ConfigManager cfgMgr = config.getOwner();
        Boolean showX = (Boolean) cfgMgr.getConfigSetting(Settings.AXLOC_SHOWX);
        this.showXChb.setSelected(showX.booleanValue() );
        this.showYChb.setSelected(true);
        Boolean showLb = (Boolean) cfgMgr.getConfigSetting(Settings.AXLOC_SHOWLB);
        this.showLabelChb.setSelected(showLb.booleanValue() );
        //programming set checkbox won't fire action event, need programmatically invoke the methods
        showYChbActionPerformed(null);
        showXChbActionPerformed(null);
        showLabelChbActionPerformed(null);

    }
    private void updateIntervalHeight() {
          // Add your handling code here:
        AxisList yaxisList=config.getYaxisList() ;
        AxisPlaceTool axisPlCmd = config.getAxisPlCmd();

        //Height
        String height=this.axisHeightFd.getText().trim() ;
        int h=(int)Validator.getNumberByText(height,this);
        int curH=yaxisList.getAxis(0).getActiveSize() ;
        if(h!=curH)
            axisPlCmd.setYAxisHeight(h,false);

        //Width
        String itvs=this.axisIntervalFd.getText().trim() ;
        int itv=(int)Validator.getNumberByText(itvs,this);
        axisPlCmd.setAllAsDefaultInteval(itv,false);

        axisPlCmd.updatePlotForChangeSize() ;

        this.axisHeightFd.setText(h+"");
        this.axisIntervalFd.setText(itvs);
    }
    private void update(String key,Object value){
         //PlotTool plotCmd=(PlotTool)owner.getCmd(BasicTool.PlotT );
        ConfigManager owner = this.config.getOwner();
        PlotTool plotCmd=(PlotTool) owner.getCmd(BasicTool.PlotT )  ;
        plotCmd.setAConfigInfo("axis",key,value ) ;
     }
    /*******************************************************************************************************
     *                GUI
     *******************************************************************************************************/

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
     private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        sizeP = new javax.swing.JPanel();
        axisIntervalLb = new javax.swing.JLabel();
        axisIntervalFd = new javax.swing.JTextField();
        applyBtn = new javax.swing.JButton();
        axisHeightLb = new javax.swing.JLabel();
        axisHeightFd = new javax.swing.JTextField();
        showHideP = new javax.swing.JPanel();
        showYChb = new javax.swing.JCheckBox();
        showXChb = new javax.swing.JCheckBox();
        showLabelChb = new javax.swing.JCheckBox();

        setLayout(new java.awt.GridLayout(1, 3));

        sizeP.setLayout(new java.awt.GridBagLayout());

        sizeP.setBorder(new javax.swing.border.TitledBorder(null, "Axes Size", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("MS Sans Serif", 1, 14)));
        axisIntervalLb.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        axisIntervalLb.setText("Axes Interval");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 3, 9);
        sizeP.add(axisIntervalLb, gridBagConstraints);

        axisIntervalFd.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        sizeP.add(axisIntervalFd, gridBagConstraints);

        applyBtn.setText("Apply");
        applyBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyBtnActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        sizeP.add(applyBtn, gridBagConstraints);

        axisHeightLb.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
        axisHeightLb.setText("Axes Height");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 8);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        sizeP.add(axisHeightLb, gridBagConstraints);

        axisHeightFd.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
        sizeP.add(axisHeightFd, gridBagConstraints);

        add(sizeP);

        showHideP.setLayout(new java.awt.GridLayout(3, 0));

        showHideP.setBorder(new javax.swing.border.TitledBorder(null, "Show/Hide Axes", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("MS Sans Serif", 1, 14)));
        showYChb.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
        showYChb.setText("Show Y Axes");
        showYChb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showYChbActionPerformed(evt);
            }
        });

        showHideP.add(showYChb);

        showXChb.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
        showXChb.setText("Show X Axis");
        showXChb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showXChbActionPerformed(evt);
            }
        });

        showHideP.add(showXChb);

        showLabelChb.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
        showLabelChb.setText("Show Labels");
        showLabelChb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showLabelChbActionPerformed(evt);
            }
        });

        showHideP.add(showLabelChb);

        add(showHideP);

    }

    private void showLabelChbActionPerformed(java.awt.event.ActionEvent evt) {
        boolean shl=this.showLabelChb.isSelected() ;
        this.update(AxisLayer.AxisLayerConfigInfo.ShowLabel , new Boolean(shl));
    }

    private void showXChbActionPerformed(java.awt.event.ActionEvent evt) {
        boolean shx=this.showXChb.isSelected() ;
        this.update(AxisLayer.AxisLayerConfigInfo.ShowXAxis , new Boolean(shx));
    }

    private void showYChbActionPerformed(java.awt.event.ActionEvent evt) {
          boolean shy=this.showYChb.isSelected() ;
        this.update(AxisLayer.AxisLayerConfigInfo.ShowYAxis , new Boolean(shy));
    }



    private void applyBtnActionPerformed(java.awt.event.ActionEvent evt) {
       updateIntervalHeight() ;
    }




    // Variables declaration - do not modify
    private javax.swing.JButton applyBtn;
    private javax.swing.JTextField axisHeightFd;
    private javax.swing.JLabel axisHeightLb;
    private javax.swing.JTextField axisIntervalFd;
    private javax.swing.JLabel axisIntervalLb;
    private javax.swing.JPanel showHideP;
    private javax.swing.JCheckBox showLabelChb;
    private javax.swing.JCheckBox showXChb;
    private javax.swing.JCheckBox showYChb;
    private javax.swing.JPanel sizeP;
    // End of variables declaration


}

